package com.su27.web;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.su27.document.ConverterUtils;
import com.su27.document.DocumentData;
import com.su27.document.TestConverter;
import com.su27.document.config.ConfigReader;

public class DocService {
	private static String DOC_PATH = null;
	private static String PIC_PATH = null;
	private static String OUT_PATH = null;
	private static String USER_NAME = null;
	private static String USER_PASSWORD = null;
	private static String USER_IMSI = null;
	static {
		try {
			DOC_PATH = ConfigReader.getString("doc.path");
			PIC_PATH = ConfigReader.getString("pic.path");
			OUT_PATH = ConfigReader.getString("out.path");
			USER_NAME = ConfigReader.getString("user.name");
			USER_PASSWORD = ConfigReader.getString("user.password");
			USER_IMSI = ConfigReader.getString("user.imsi");
		
				
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public DocRespBean login(DocReqBean reqBean) {
		DocRespBean respBean = new DocRespBean();

		if ( USER_NAME.equals(reqBean.getUsername()) &&
				USER_PASSWORD.equals(reqBean.getPassword()) &&
				USER_IMSI.equals(reqBean.getImsi())
				 ) {
			respBean.setResult(true);
			respBean.setInfo("abcdefg");
		}
		 else { respBean.setResult(false); respBean.setInfo("login fail"); }
		 

		return respBean;
	}

	public DocRespBean getFileList(DocReqBean reqBean) {
		DocRespBean respBean = new DocRespBean();

		List<Doc> docList = DocumentData.getDocList();

		Doc[] docs = new Doc[docList.size()];
		for (int i = 0; i < docList.size(); i++) {
			docs[i] = docList.get(i);
		}
		respBean.setResult(true);
		respBean.setDoc(docs);
		respBean.setCount(docs.length);
		return respBean;
	}

	public DocRespBean getFile(DocReqBean reqBean) {
		DocRespBean respBean = new DocRespBean();

		String id = reqBean.getId();
		Map<String, String> docMap = DocumentData.getDocMap();
		String name = docMap.get(id);
		respBean.setResult(true);
		respBean.setId(id);
		respBean.setName(name);

		try {
			int pages = ConverterUtils.getFilePage(DocumentData.doc_path
					+ File.separator + name);
			respBean.setPage(pages);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String srcfile = DOC_PATH + File.separator + name;
		String picPath = PIC_PATH + File.separator + id;
		String outputPath = OUT_PATH + File.separator + id;
		File outputFile = new File(outputPath);
		if (!outputFile.exists() || !outputFile.isDirectory()) {
			TestConverter convert = new TestConverter();
			convert.convert(srcfile, picPath, outputPath);

		}
		
	
		respBean.setPath("/files/"+id+"/");
		respBean.setKey("ABCDEFGH");

		return respBean;
	}

	public DocRespBean log(DocReqBean reqBean) {
		DocRespBean respBean = new DocRespBean();

		if (reqBean.getLog() != null && reqBean.getLog().length() > 0) {
			System.out.println(reqBean.getLog());
			respBean.setResult(true);
			respBean.setInfo("upload success");
		} else {
			respBean.setResult(false);
			respBean.setInfo("no log");
		}

		return respBean;
	}

}
